<?php

namespace Morrow;

class PageController extends DefaultController {
	public function run() {




		$wid = $this->input->get('wid');

		if(!$wid){
			$this -> url -> redirect('home');
		}

		$this->load('Db', $this->config->get('db'));	
		$sql_webapps=$this->db->result('
			SELECT *
			FROM webapps
			WHERE wid = ?'
			, $wid);

		$webapps_result=$sql_webapps['RESULT'][0];

		$code=$sql_webapps['RESULT'][0]["code"];

		if($webapps_result['showcontact']==1){

			$sql_users=$this->db->result('
				SELECT *
				FROM users
				WHERE uid = ?'
				, $sql_webapps['RESULT'][0]['uid']);

			$addresse_result=$sql_users['RESULT'][0];

			$this->Vcard->data['display_name'] = $addresse_result['name'];
			$this->Vcard->data['company'] = $addresse_result['org'];
			$this->Vcard->data['office_tel'] = $addresse_result['tel'];
			$this->Vcard->data['cell_tel'] = $addresse_result['mobile'];
			$this->Vcard->data['email1'] = $addresse_result['cardemail'];
			$this->Vcard->data['work_address'] = $addresse_result['adresse'];
			$this->Vcard->data['work_postal_code'] = $addresse_result['zip'];
			$this->Vcard->data['work_city'] = $addresse_result['city'];
			$this->Vcard->data['work_country'] = $addresse_result['country'];

			//check andriod
			if($this->Vcard->get_device_type()=='android'){


				$this->Vcard->download(); die();

			}

			//check IOS
			//if($this->Vcard->get_device_type()=='ios'){
			elseif($this->Vcard->get_device_type()=='ios'){
				$time=date('U');

				$filename="main/assets/vcard/".$time.".vcf";
				$fp=fopen($filename,"w+");//fopen()的其它开关请参看相关函数
				$str="BEGIN:VCARD\r\nVERSION:3.0\r\nN:".$addresse_result['name']."\r\nORG:".$addresse_result['org']."\r\nTEL;TYPE=WORK,VOICE:".$addresse_result['tel']."\r\nTEL;TYPE=HOME,VOICE:".$addresse_result['mobile']."\r\nEMAIL;TYPE=PREF,INTERNET:".$addresse_result['cardemail']."\r\nADR;TYPE=WORK:;;".$addresse_result['adresse'].";".$addresse_result['zip']." ".$addresse_result['city']." ".$addresse_result['country']."\r\nEND:VCARD";
				fputs($fp,$str);
				fclose($fp); 

				header("Content-type: text/x-vcalendar; charset=utf-8"); 
				header("Content-Disposition: attachment; filename=\"iphonecontact.ics\";");
				echo "BEGIN:VCALENDAR\n";
				echo "VERSION:2.0\n";
				echo "BEGIN:VEVENT\n";
				echo "SUMMARY:请点击附件获得联系人信息\n";
				$dtstart = date("Ymd")."T".date("Hi")."00";
				echo "DTSTART;TZID=Europe/London:".$dtstart."\n";
				$dtend = date("Ymd")."T".date("Hi")."01";
				echo "DTEND;TZID=Europe/London:".$dtend."\n";
				echo "DTSTAMP:".$dtstart."Z\n";
				echo "ATTACH;VALUE=BINARY;ENCODING=BASE64;FMTTYPE=text/directory;\n";
				echo " X-APPLE-FILENAME=".$addresse_result['name'].".vcf:\n";
				$file=$filename; 
				$vcard = file_get_contents($file);       # read the file into memory
				$b64vcard = base64_encode($vcard);                     # base64 encode it so that it can be used as an attachemnt to the "dummy" calendar appointment
				$b64mline = chunk_split($b64vcard,74,"\n");             # chunk the single long line of b64 text in accordance with RFC2045 (and the exact line length determined from the original .ics file exported from Apple calendar
				$b64final = preg_replace('/(.+)/', ' $1', $b64mline);   # need to indent all the lines by 1 space for the iphone (yes really?!!)
				echo $b64final;                                         # output the correctly formatted encoded text
				echo "END:VEVENT\n";
				echo "END:VCALENDAR\n";
				die;
								
			}
			else{
				$link="webapp/".$code;

				$this -> url -> redirect($link);

			}


		}
	
	}	
}